import React, { useEffect, useState, useRef } from 'react';
//闭包陷阱:有定时器不能重新跑 effect 函数
//解决方案3:用useRef
export default function Seven() {
  const [count, setCount] = useState(0);
  const updateCount = () => {
    setCount(count + 1);
  };
  const ref = useRef(updateCount);
  ref.current = updateCount; //值改了不会触发渲染
  useEffect(() => {
    const timer = setInterval(() => ref.current(), 1000);
    return () => {
      clearInterval(timer);
    };
  }, []);
  return <div>{count}</div>;
}
//useRef适合保存渲染过程中一些数据的场景
